/**
 * Created by root on 2017-08-10.
 */
import React, { Component, PropTypes } from 'react';
import { Utility, TaskItem, GxIcon, ApiInfo } from 'components';
import { connect } from 'react-redux';
import * as CommonActions from 'redux/modules/reduxCommon';

@connect(
  state => ({
    Title: state.Common.Title,                                          // 标题
    UrlParams: state.Common.UrlParams,                                  // URL参数
    StatisticInfo: state.Common.StatisticInfo,                          // 统计数据
    TaskAssignInfo: state.Common.TaskAssignInfo                         // 任务分配数据
  }),
  { ...CommonActions })

export default class GmessageBoard extends Component {
  static propTypes = {
    Title: PropTypes.string,                                            // 标题
    UrlParams: PropTypes.object,                                        // url 参数
    StatisticInfo: PropTypes.object,                                    // 统计数据
    TaskAssignInfo: PropTypes.object,                                   // 任务分配数据
    onApiGet: PropTypes.func,                                           // Get请求
    onUpdateRedux: PropTypes.func,                                      // 更新Redux
    onSetContent: PropTypes.func,                                       // 保存数据
  };

  constructor(props) {
    super(props);
    this.state = {
      __Index: 0,
      IsCollection: true,                                              // 是否收藏
      IsNotData: false,                                                // 有无数据
    };
  }

  componentDidMount() {
    this.state.IsMount = true;
    this.__InitData();
  }

  componentWillUnmount() {
    delete this.state.IsMount;
  }

  /**
   * 初始化页面数据
   * @returns 
   * @memberof GmessageView
   */
  __InitData() {
    const { onApiGet, onUpdateRedux, TaskAssignInfo } = this.props;
    if (!Utility.isFunction(onApiGet)) {
      return;
    }
    const __params = Object.assign({}, { pg_index: 0, pg_count: 5 });
    const __self = this;
    const __ApiList = [];
    const { List } = TaskAssignInfo || {};
    if (document.body.scrollTop === 0 || !Utility.isArray(List)) {
      __ApiList.push(onApiGet('TaskAssignInfo', ApiInfo.JXKB.TaskAssigns, { params: __params }));
    }
    __ApiList.push(onApiGet('StatisticInfo', ApiInfo.JXKB.TaskTotal, { params: {} }));
    Utility.$loading();
    Promise.all(__ApiList).then(() => {
      onUpdateRedux();
      __self.__JudgeIsExistData();
      __self.__UpdateRender();
      Utility.$loadingHide();
    }).catch((ex) => {
      __self.__JudgeIsExistData();
      __self.__UpdateRender();
      Utility.$loadingHide();
      console.log(ex);
    });
  }

  /**
   * 判断有无数据列表
   * 
   * @returns 
   * @memberof GmessageView
   */
  __JudgeIsExistData() {
    const { TaskAssignInfo } = this.props;
    if (TaskAssignInfo) {
      if (Utility.isArray(TaskAssignInfo.List)) {
        return;
      }
    }

    this.state.IsNotData = true;
  }

  /**
   * 更新渲染
   */
  __UpdateRender() {
    if (!!this.state.IsMount) {
      this.setState({ __CurrentTempDate: new Date().getTime() });
    }
  }

  /**
   * 更新state
   * 
   * @param {any} type 
   * @memberof GmessageBoard
   */
  __UpdateState(type) {
    if (type) {
      this.state[type] = !this.state[type];
    }
    this.__UpdateRender();
  }

  /**
   * 页面跳转。
   * @param params
   * @private
   */
  __HandlerGoToPage(url, params) {
    Utility.toPage(url, params);
  }

  /**
   * 任务列表
   * 
   * @param {any} styles 
   * @returns 
   * @memberof TaskAssign
   */
  __TaskAssignListHtml(styles) {
    const { TaskAssignInfo } = this.props;
    const { List } = TaskAssignInfo || {};
    if (!Utility.isArray(List)) {
      return null;
    }
    const __list = List.slice(0, 5) || [];
    if (!Utility.isArray(__list)) {
      return null;
    }
    let goIng = [];
    let unTreat = [];
    return __list.map((item, index) => {
      const { userName, statusNums } = item || {};
      const _TaskItems = {};
      // 进行中
      goIng = statusNums.filter((go) => {
        return go.status === 'RECEIVED';
      });
      // 待处理
      unTreat = statusNums.filter((treat) => {
        return treat.status === 'START';
      });
      _TaskItems.userName = userName;
      _TaskItems.goIng = goIng;
      _TaskItems.unTreat = unTreat;
      const condition = {};
      condition.userName = userName;
      condition.type = 'wo@';
      condition.status = ['RECEIVED', 'START'];
      return (
        <div className={styles.listCss + ' ' + (index === __list.length - 1 ? styles.lastItem : '')}
          key={'list_item_' + index} onClick={this.__HandlerToSearch.bind(this, condition)}>
          <TaskItem TaskItems={_TaskItems} />
        </div>
      );
    });
  }

  /**
   * 跳转-搜索页面 搜索条件: userName && status: [RECEVIED,START]
   * @param {*} url 
   * @param {*} params 
   */
  __HandlerToSearch(condition) {
    const { UrlParams, onSetContent } = this.props;
    const { token } = UrlParams || {};
    let __token = '';
    if (token) {
      __token = token;
    }
    const obj = Object.assign({}, condition);
    onSetContent('TaskAssignCondition', obj);
    const { SearchPage } = Utility.constItem.UrlItem;
    if (SearchPage) {
      Utility.toPage(SearchPage, { token: __token });
    }
  }

  render() {
    const styles = require('./scss/GmessageBoard.scss');
    const { StatisticInfo, UrlParams } = this.props;
    const { dayNum, tomorrowNum } = StatisticInfo || {};
    const { TaskAssign } = Utility.constItem.UrlItem;
    const { token } = UrlParams || {};
    let __token = '';
    if (token) {
      __token = token;
    }
    const _CurrentDay = new Date();
    // 查看-今日/明日任务条件
    const lookCondition = {};
    lookCondition.type = '@wo';
    lookCondition.begin_deadline = _CurrentDay.getTime();
    lookCondition.end_deadline = _CurrentDay.getTime() + 86400000;
    lookCondition.status = ['RECEIVED', 'START'];

    return (
      <div className={styles.messageBoardCss}>
        <div className={styles.messageCss}>
          <div className={styles.topCss}>
            <div className={styles.leftCss}>
              <GxIcon IconType="iconBlueTime" IsHidePadding /><span className={styles.private}>即将到期</span>
            </div>
            <div className={styles.rightCss} onClick={this.__HandlerToSearch.bind(this, lookCondition)}>查看</div>
          </div>
          <div className={styles.downCss}>
            <div className={styles.leftContainer}>
              <div className={styles.todayNum}>{dayNum || '0'}<span className={styles.slip}>/条</span></div>
              <div className={styles.today}>今日</div>
            </div>
            <div className={styles.rightContainer}>
              <div className={styles.tomorrowNum}>{tomorrowNum || '0'}<span className={styles.slip}>/条</span></div>
              <div className={styles.tomorrow}>明日</div>
            </div>
          </div>
        </div>
        <div className={styles.taskAssignCss}>
          <div className={styles.topCss}>
            <div className={styles.leftCss}>
              <GxIcon IconType="iconAssign" IsHidePadding /><span className={styles.private}>任务分配</span>
            </div>
            <div className={styles.rightCss} onClick={this.__HandlerGoToPage.bind(this, TaskAssign, { token: __token })}>全部</div>
          </div>
          <div className={styles.container}>
            {this.__TaskAssignListHtml(styles)}
          </div>
        </div>
      </div>
    );
  }
}

